package com.atguigu.eduservice.controller.front;


import com.atguigu.common.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师前台显示接口
 * </p>
 *
 * @author testjava
 * @since 2020-04-13
 */
@RestController
@RequestMapping("/eduservice/frontteacher")
@Api(description="名师首页")
//@CrossOrigin
public class FrontEduTeacherController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService teacherService;

    //1 分页查询的方法
    @GetMapping("getFrontTeacherList/{page}/{limit}")
    @ApiOperation(value = "获取所有讲师信息并分页")
    public R getFrontTeacherList(@PathVariable long page,@PathVariable long limit) {
        Page<EduTeacher> pageParam = new Page<>(page,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        teacherService.page(pageParam,wrapper);

        //因为前端页面使用普通方式实现分页，需要把分页所有数据传递过去
        //把分页所有数据放到map集合，进行传递
        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return R.ok().data(map);
    }

    //2 根据id查询讲师详情信息
    @GetMapping("getFrontTeacherInfo/{id}")
    @ApiOperation(value = "点击老师头像即可获取id并根据id获取讲师详情")
    public R getFrontTeacherInfo(@PathVariable String id) {
        //1 根据讲师id查询讲师基本信息
        EduTeacher teacher = teacherService.getById(id);

        //2 根据讲师id查询所讲课程信息
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",id);
        List<EduCourse> courseList = courseService.list(wrapper);

        return R.ok().data("teacher",teacher).data("courseList",courseList);
    }
}

